#include"head.h"

size_t WordOffset = 0;
std::ifstream fin;
std::vector<char*> Spilited;

char* buffer;
char FileName[Max_Path_len];
std::vector<element> Container;


using namespace std;

void Split(char* buffer) {
	char* token;
	int safe = 32, point = 0;
	token = strtok(buffer, Str_delim);

	while ((token && token[0] != '\0') && ++point < safe) {
		Spilited.push_back(token);
		token = strtok(nullptr, Str_delim);
	}
}

int WhichOne(char* word) {
	map<string, int>::iterator iter;
	iter = dic.find(word);
	if (iter != dic.end()) return iter->second;

	return NotInDic(word);
}

void StorToBuf(vector<char*>& Splited) {

	for (int i = 0; i < Spilited.size(); i++) {
		strcpy(Container[WordOffset].have, Spilited[i]);
		Container[WordOffset].belong = WhichOne(Container[WordOffset].have);
		WordOffset++;
	}
}

void StorInFile(vector<element>& container) {
	ofstream out;
	out.open(Stored_File_Name, ios::out);

	for (int i = 0; i < WordOffset; i++) {
		out << "(" << container[i].have << ", " << container[i].belong << ")\n";
	}

	out.close();
}

void OpenFile(string filename) {

	Container.resize(Max_Container_Size);

	fin.open(filename, ios::in);
	if (!fin.is_open()) exit(-1);

	while (!fin.eof()) {
		char tbuffer[Max_Line_len] = "";
		buffer = tbuffer;

		fin.getline(buffer, Max_Line_len);

		UnComent(buffer);

		Split(buffer);
		StorToBuf(Spilited);

		Spilited.clear();

	}

	IllegalChar(Container);
	StorInFile(Container);

	fin.close();
}

////////////////////////////////////

int NotInDic(char* word) {
	if (IsNumber(word)) return T_NUB;
	return SplitTAG(word);
}





